package com.patrol.logisticsmanage.ui.canteen;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.patrol.logisticsmanage.R;
import com.patrol.logisticsmanage.ui.canteen.bean.Restaurant;

import java.util.List;

public class CanteenAdapter extends RecyclerView.Adapter<CanteenAdapter.CanteenViewHolder> {
    Context context;
    List<Restaurant> restaurants;
    OnItemClick onItemClick;

    public CanteenAdapter(Context context, List<Restaurant> restaurants,OnItemClick onItemClick) {
        this.context = context;
        this.restaurants = restaurants;
        this.onItemClick = onItemClick;
    }

    @NonNull
    @Override
    public CanteenViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_canteen, parent, false);
        return new CanteenViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CanteenViewHolder holder, final int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClick.onClick(position);
            }
        });

        Restaurant restaurant = restaurants.get(position);
        holder.tv_name.setText(restaurant.getName());
        holder.tv_sub_title.setText(restaurant.getPrincipal());

        Glide.with(context)
                .load(restaurant.getPicture())
                .centerCrop()
                .placeholder(R.drawable.icon_shop)
                .into(holder.img_canteen);

    }

    @Override
    public int getItemCount() {
        return restaurants.size();
    }

    class CanteenViewHolder extends RecyclerView.ViewHolder{

        ImageView img_canteen;
        TextView tv_name;
        TextView tv_stars;
        TextView tv_sale;
        TextView tv_sub_title;

        public CanteenViewHolder(@NonNull View itemView) {
            super(itemView);
            img_canteen = itemView.findViewById(R.id.img_canteen);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_stars = itemView.findViewById(R.id.tv_stars);
            tv_sale = itemView.findViewById(R.id.tv_sale);
            tv_sub_title = itemView.findViewById(R.id.tv_sub_title);

        }
    }

    interface OnItemClick{
       void onClick(int position);
    }
}
